package com.douguo.recipe;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.douguo.recipe.manager.AppRuntime;
import com.douguo.recipe.network.DownloadPackage;
import com.douguo.recipe.view.NavigationView;
import com.exchange.Controller.ReportThread;
import com.exchange.Model.AdvertiserConfig;
import com.exchange.Public.DeviceManager;
import com.exchange.Public.DownloadAgent;
import com.exchange.Public.ExchangeConstants;
import com.exchange.Public.RetriveImageThread;
import com.exchange.View.DownloadDialog;
import com.exchange.View.EmbededContainer;
import com.exchange.View.ResourceManager.IdMapper;

public class Tools2Activity extends DouguoActivity {
	// www.douguo.com/apk/AndroidOptimizer_2.0.1_douguo.apk
	private Dialog downloadDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exchange_list_layout);

		new NavigationView(this, NavigationView.STATE_MORE);

		findViewById(R.id.imageButton_back).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {
						finish();
					}
				});
		setTitle(R.string.tools);
		new EmbededContainer(Tools2Activity.this, findViewById(R.id.ad),
				(ListView) findViewById(R.id.recipeslist),
				ExchangeConstants.CONTAINER_LIST_COUNT);

		View header = View.inflate(getApplicationContext(),
				R.layout.exchange_container_banner, null);
		header.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.FILL_PARENT, 90));
		
		header.setBackgroundColor(android.R.drawable.list_selector_background);

		downloadDialog = getCustomeDialog(this);

		header.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				downloadDialog.show();
			}
		});

		ImageView icon = (ImageView) header.findViewById(R.id.exchange_icon);
		icon.setImageResource(R.drawable.opda_icon);

		TextView name = (TextView) header.findViewById(R.id.exchange_name);
		name.setText("安卓优化大师");

		TextView exchange_des = (TextView) header
				.findViewById(R.id.exchange_des);
		exchange_des
				.setText("安卓优化大师一款集15个优秀小工具的手机提速优化工具箱，包含体检，程序分类，快捷设置，进程终结，批量安装和卸载，手机垃圾清理，节电设置，文件管理，手机硬件参数等。帮助你更好的使用自己爱机。");

		if (AppRuntime.showOPDA) {
			((ListView) findViewById(R.id.recipeslist)).addHeaderView(header);
		}
	}

	public AlertDialog getCustomeDialog(final Context mContext) {

		AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
		final AlertDialog aDialog = ab.create();

		LayoutInflater li = (LayoutInflater) mContext
				.getSystemService("layout_inflater");
		View view = li.inflate(R.layout.exchange_download_dialog, null);

		aDialog.setView(view, 0, 0, 0, 0);

		TextView developer = (TextView) view.findViewById(IdMapper.dev());
		developer.setText("开发：opda");

		TextView appname = (TextView) view.findViewById(IdMapper.appname());
		appname.setText("名称：安卓优化大师");

		TextView size = (TextView) view.findViewById(IdMapper.size());
		size.setVisibility(View.INVISIBLE);

		TextView detail = (TextView) view.findViewById(IdMapper.des0());
		detail.setText("安卓优化大师一款集15个优秀小工具的手机提速优化工具箱，包含体检，程序分类，快捷设置，进程终结，批量安装和卸载，手机垃圾清理，节电设置，文件管理，手机硬件参数等。帮助你更好的使用自己爱机。");

		ImageView photo = (ImageView) view.findViewById(IdMapper.photo());
		photo.setImageResource(R.drawable.opda_icon);

		Button okBut = (Button) view.findViewById(IdMapper.ok());
		okBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				DownloadPackage dp = new DownloadPackage(mContext,
						"http://www.douguo.com/apk/AndroidOptimizer_2.0.1_douguo.apk",
						"正在下载应用", "正在下载应用-安卓优化大师");
				aDialog.cancel();
			}
		});
		Button cancelBut = (Button) view.findViewById(IdMapper.cancel());
		cancelBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				aDialog.cancel();
			}

		});
		RelativeLayout exchangeDetail = (RelativeLayout) view
				.findViewById(IdMapper.exchange_detail0());

		RelativeLayout.LayoutParams layout = (RelativeLayout.LayoutParams) exchangeDetail
				.getLayoutParams();
		int contentHeight = DeviceManager.dpToPix(
				(DeviceManager.isScreenPortrait(mContext)) ? 240 : 120,
				mContext);
		layout.height = contentHeight;

		exchangeDetail.setLayoutParams(layout);

		return aDialog;
	}
}
